<template>
  <div>
    <v-search></v-search>
    <div class="mui-content content">
      <!--<div is="v-slider"></div>-->
      <v-slider :list="slider"></v-slider>
      <v-hot :list="hot"></v-hot>
      <v-group></v-group>
      <v-news></v-news>
    </div>
  </div>
</template>
<script>
import $ from "jquery";
import "swiper";
import { mapState } from 'vuex'
import { mapActions } from 'vuex'
import { mapGetters } from 'vuex'
import { FETCH_HOT, FETCH_SLIDER } from '../constants/actionTypes'
import ajax from '../utils/ajax'
import tool from '../utils/tool'
import vSearch from '../components/home/search.vue'
import vSlider from '../components/home/slider.vue'
import vHot from '../components/home/hot.vue'
import vGroup from '../components/home/group.vue'
import vNews from '../components/home/news.vue'
export default {
  mixins: [],
  data() {
    return {
      scroll: true,
      news: [],
      index: {},
      searchDataStr: ''
    };
  },
  mounted() {

    this.getSliderLoad();

    this.gethotLoad();
  },
  created() {

  },
  beforeRouteLeave(to, from, next) {
    // 如果跳转到详情页面，则记录关键数据
    // 方便从详情页面返回到该页面的时候继续加载之前位置的数据
    // if (from.name === 'home') {
    //   // 当前滚动条位置
    //   window.window.sessionStorage.scrollTop = $(window).scrollTop();
    //   // 当前页面头条数据
    //   window.window.sessionStorage.news = JSON.stringify(this.list.newsload);
    //   // 查询参数
    //   window.window.sessionStorage.searchKey = JSON.stringify(this.searchKey);
    // }
    next()
  },
  beforeRouteEnter(to, from, next) {
    // if (from.name !== 'search') {
    //   // 页面切换移除之前记录的数据集
    //   window.window.sessionStorage.removeItem('news');
    // }
    next()
  },
  computed: {
    // 使用对象展开运算符将 getters 混入 computed 对象中
    ...mapGetters({
      slider: 'sliderLoad',
      hot: 'hotLoad'
    }),
    //使用 mapState 辅助函数帮助我们生成计算属性
    ...mapState({
      list: state => state.home
    })
  },
  methods: {
    ...mapActions([FETCH_SLIDER, FETCH_HOT]),
    getSliderLoad() {
      var vm = this;
      let setting = {
        url: "phoneAppManager/getAppInfosByCode",
        data: { code: '600002', start: 0, iDisplayLength: 4 },
        callback: "succerSlider",
        error: function(res) {
          // console.log(res)[FETCH_SLIDER, FETCH_HOT]
        }
      }
      new ajax(setting);
      window.succerSlider = (res) => {
        res = tool.clear(res);
        res = tool.JSONParse(res);

        if (res.ret !== 1) {
          alert("失败");
        } else {
          vm.FETCH_SLIDER(res.data.appModuleInfos)
        }
        // DOM渲染后执行
        vm.$nextTick(function() {
          var mySwiper = new Swiper('.swiper-container', {
            loop: true,
            autoplay: 3000,
            pagination: '.swiper-pagination', // 分页器
            paginationClickable: true // 分页器指示点控制Swiper切换
          });
        })
      }
    },
    gethotLoad() {
      var vm = this;
      let setting = {
        url: "weixinApp/hotWeixin",
        data: {},
        callback: "succerHot",
        error: function(res) {
          // console.log(res)
        }
      }
      new ajax(setting);
      window.succerHot = function(res) {
        res = tool.clear(res);
        res = tool.JSONParse(res);
        if (res.ret !== 1) {
          alert("失败");
        } else {
          vm.FETCH_HOT(res.data.result)
        }
      }
    }

  },
  watch() {

  },
  components: {
    vSearch,
    vSlider,
    vHot,
    vGroup,
    vNews
  }
}

</script>

<style lang="less" scoped>

</style>